package com.lcx.plugin.neteaseCloud.component.toolwindow.login;

import com.intellij.openapi.ui.ComboBox;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.lcx.plugin.neteaseCloud.component.toolwindow.CustomNodeRenderer;
import com.lcx.plugin.neteaseCloud.component.toolwindow.login.email.EmailLoginDialogWrapper;
import com.lcx.plugin.neteaseCloud.component.toolwindow.login.phone.PhoneLoginDialogWrapper;
import com.lcx.plugin.neteaseCloud.constant.IconConstant;
import com.lcx.plugin.neteaseCloud.enums.LoginTypeEnum;
import com.lcx.plugin.neteaseCloud.music.DataCenter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Objects;

/**
 * @author wengyongcheng
 * @since 2020/3/2 10:36 下午
 */
public class LoginTypeDialogWrapper extends DialogWrapper {

    private ComboBox comboBox;

    private CustomOKAction okAction;
    private DialogWrapperExitAction exitAction;

    public LoginTypeDialogWrapper() {
        super(true);
        init();
        setTitle("登录 (1/3)");
    }


    /**
     * 创建视图
     *
     * @return
     */
    @Nullable
    @Override
    protected JComponent createCenterPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        comboBox = new ComboBox();
        comboBox.addItem("--请选择登录方式--");

        comboBox.addItem("邮箱");
        comboBox.addItem("手机");
        comboBox.addItem("验证码");
        comboBox.addItem("二维码");
        panel.add(comboBox, BorderLayout.NORTH);

        /**
         * 重点：设置节点渲染器
         */
        Icon[] icons = new Icon[]{IconConstant.SELECT, IconConstant.EMAIL, IconConstant.PHONE, IconConstant.PHONE_CODE, IconConstant.QR_CODE};
        Icon[] selectIcons = new Icon[]{IconConstant.SELECT_SELECT, IconConstant.EMAIL_SELECT, IconConstant.PHONE_SELECT,
                IconConstant.PHONE_CODE_SELECT, IconConstant.QR_CODE_SELECT};
        comboBox.setRenderer(new CustomNodeRenderer(icons, selectIcons));

        return panel;

    }

    /**
     * 覆盖默认的ok/cancel按钮
     *
     * @return
     */
    @NotNull
    @Override
    protected Action[] createActions() {
        exitAction = new DialogWrapperExitAction("取消", CANCEL_EXIT_CODE);
        okAction = new CustomOKAction();
        // 设置默认的焦点按钮
        okAction.putValue(DialogWrapper.DEFAULT_ACTION, true);
        return new Action[]{exitAction, okAction};
    }

    /**
     * 自定义 ok Action
     */
    protected class CustomOKAction extends DialogWrapperAction {

        protected CustomOKAction() {
            super("下一步");
        }

        @Override
        protected void doAction(ActionEvent e) {
            // 点击ok的时候进行数据校验
            int selectedIndex = comboBox.getSelectedIndex();
            DataCenter.loginType = selectedIndex;
            Object selectedItem = comboBox.getSelectedItem();
            System.out.println("登录方式为：" + selectedItem);
            if (Objects.equals(selectedIndex, 0)) {
                Messages.showMessageDialog("请选择一种登录方式", "提示", Messages.getInformationIcon());
            } else if (Objects.equals(selectedIndex, LoginTypeEnum.EMAIL.getCode())) {
                close(CANCEL_EXIT_CODE);
                new EmailLoginDialogWrapper().show();
            } else if (Objects.equals(selectedIndex, LoginTypeEnum.PHONE_CODE.getCode())
                    || Objects.equals(selectedIndex, LoginTypeEnum.PHONE_NUMBER.getCode())) {
                close(CANCEL_EXIT_CODE);
                new PhoneLoginDialogWrapper().show();
            } else {
                // 二维码登录
                close(CANCEL_EXIT_CODE);
//                new QrCodeDialogWrapper().show();
            }
        }
    }
}
